﻿using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Web.Mvc;
using Matrix.MaKaYunShang.BLL;
using Matrix.MaKaYunShang.Model;
using Matrix.MaKaYunShang.Model.Dto;

namespace Matrix.MaKaYunShang.MagWeb.Controllers
{
    [Authorization]
    [Matrix.MaKaYunShang.Model.Attributes.Skip]
    public class CommodityPictureController : BaseController
    {
        private readonly CommodityPictureBLL _bll = new CommodityPictureBLL();

        #region 列表

        public ActionResult List()
        {
            return View();
        }

        [HttpPost]
        [Matrix.MaKaYunShang.Model.Attributes.Skip]
        public JsonResult Listing(int draw, int start, int length)
        {
            int order;
            int.TryParse(Request["order[0][column]"], out order);
            string direction = Request["order[0][dir]"];
            var orderByColumns = new[] {"CommodityName", "PicUrl", "DisplaySort"};
            string search = Request["search[value]"];
            int pageIndex = (start/length) + 1;

            TableDataViewDto<ViewCommodityPictureDto> theList = _bll.GetPagingList(draw, pageIndex, length,
                SqlSafe(search),
                orderByColumns[order] + " " + direction);

            JsonResult jsonResult = Json(theList);
            jsonResult.MaxJsonLength = int.MaxValue;

            return jsonResult;
        }

        #endregion

        #region 创建

        public ActionResult Create()
        {
            InitiSet();

            return View();
        }

        [HttpPost]
        public ActionResult Create(FormCollection collection, CommodityPicture model)
        {
            if (model == null) return new HttpStatusCodeResult(HttpStatusCode.BadRequest);

            string strUploadImage = model.Usage == 0
                ? UploadImage("fileImage", "/CommodityPicture/Pic/", 750, 750)
                : UploadImage("fileImage", "/CommodityPicture/Pic/", 750, 1000, "W");

            if (!string.IsNullOrEmpty(strUploadImage)) model.PicUrl = strUploadImage;

            int result = _bll.Add(model);

            if (result > 0)
                return RedirectToAction("List", "CommodityPicture");

            return new HttpStatusCodeResult(HttpStatusCode.InternalServerError);
        }

        #endregion

        #region 修改

        public ActionResult Update(int id)
        {
            InitiSet();

            if (id <= 0) return new HttpStatusCodeResult(HttpStatusCode.BadRequest);

            CommodityPicture model = _bll.GetModel(id);
            if (model == null) return new HttpStatusCodeResult(HttpStatusCode.NotFound);

            ViewBag.theImage = ConfigurationManager.AppSettings["ImgFileSitePath"] + model.PicUrl;

            return View(model);
        }

        [HttpPost]
        public ActionResult Update(FormCollection collection, CommodityPicture model)
        {
            if (model == null) return new HttpStatusCodeResult(HttpStatusCode.BadRequest);

            string strUploadImage = model.Usage == 0
                ? UploadImage("fileImage", "/CommodityPicture/Pic/", 750, 750)
                : UploadImage("fileImage", "/CommodityPicture/Pic/", 750, 1000, "W");

            if (!string.IsNullOrEmpty(strUploadImage)) model.PicUrl = strUploadImage;

            bool result = _bll.Update(model);

            if (result)
                return RedirectToAction("List", "CommodityPicture");

            return new HttpStatusCodeResult(HttpStatusCode.InternalServerError);
        }

        #endregion

        #region 删除

        [HttpPost]
        public JsonResult Delete()
        {
            string ids = Request["doIds"];
            bool result = _bll.DeleteList(ids);

            return Json(result);
        }

        #endregion

        #region Private Functions 自用函数

        private void InitiSet()
        {
            List<SelectListItem> lsCommodities =
                CommodityLibBLL.Instance.GetModelList().Select(model => new SelectListItem
                {
                    Value = model.Id.ToString(),
                    Text = model.CommodityName
                }).ToList();
            ViewBag.Commodities = lsCommodities;

            var usage = new List<SelectListItem>
            {
                new SelectListItem {Value = "0", Text = "商品头图"},
                new SelectListItem {Value = "1", Text = "详细说明用图"}
            };
            ViewBag.SelectUsage = usage;
        }

        #endregion
    }
}